import org.junit.Test;

import java.math.BigDecimal;
import java.util.function.Function;
import java.util.stream.LongStream;

public class Money {

    public Long total = 0l;

    public BigDecimal getMoney(BigDecimal money) {
        BigDecimal m = new BigDecimal(0);
        for (int i = 0; i < 20; i++) {

        }
        return m;
    }

    public static Long measureSumPerf(Function<Long, Long> adder, long n) {
        long fastest = Long.MAX_VALUE;
        for (int i = 0; i < 10; i++) {
            long start = System.nanoTime();
            long sum = adder.apply(n);
            long duration = (System.nanoTime() - start) / 1_000_000;
            System.out.println("Result: " + sum);
            if (duration < fastest) fastest = duration;
        }
        return fastest;
    }

    public static long parallelRangedSum(long n) {
        return LongStream.rangeClosed(1, n)
                .parallel()
                .reduce(0L, Long::sum);
    }

    public void add(Long l) {
        total += l;
    }

    public static Long testStream(long n) {
        Money money = new Money();
        LongStream.rangeClosed(0, n).parallel().forEach(money::add);
        return money.total;
    }

    public static void main(String[] args) {
//        System.out.println(measureSumPerf(Money::parallelRangedSum,10_000_000));
//        parallelRangedSum(10_000_000);
        System.out.println(measureSumPerf(Money::testStream, 10_000_000));

    }
}
